﻿var cusid = null;
var selIssueID = null;

function loadCusDetails(obj) {
    //alert($(obj).attr('cusid'));
    cusid = $(obj).attr('cusid');
    window.location.href = "../pages/customerdetails.aspx?cid=" + cusid;
}
function fnCusDetailsBackClick() {
    window.location.href = "../pages/customer.aspx";
}
function fnAddNewProduct() {
    $('input').val('');
    var dialogContainer = $("<div style='width:800px; height:600px; z-index:1000;'></div>");
    $('#divAddNewProductWizzard').dialog({
        modal: true,
        height: 650,
        width: 760,
        open: function () {
            $('#ui-dialog-title-divAddNewProductWizzard').text("Product Details");
            //$('#txtAreaComments').empty();
        }
    });
}
function enableTextbox() {
    $('#txtAMCInvoiceDate').removeAttr('disabled');
    $('#dateEnable').hide();
    $('#dateDisable').show();
}
function disableTextbox() {
    $('#txtAMCInvoiceDate').attr('disabled', 'disabled');
    $('#dateEnable').show();
    $('#dateDisable').hide();
}
function saveSaleWizard() {
    //alert(cGSaleID);
    isWizzard = 1;
    var sale_guid = cGSaleID;
    var sale_ID = genDynmID();
    var sale_billNo = $('#txtSaleBill').val();
    var sale_billDt = $('#txtBillDate').val();
    var sale_dealer = $('#txtDealer').val();
    var sale_modalno = $('#txtModalNo').val();
    var sale_compressorno = $('#txtCompressorNo').val();
    var sale_indoorno = $('#txtIndoorNo').val();
    var sale_outdoorno = $('#txtOutdoorNo').val();
    var sale_modalName = $('#txtModalName').val();
    //var sale_brand = $('#ddlBrands option:selected').text();
    //var sale_brand = $('#ctl00_ContentPlaceHolder1_ddlBrands option:selected').text();
    var sale_product = $('#txtProductName').val();
    //var sale_ton = $('#ddlTon option:selected').text();
    var sale_productType = $('#ddlProdCate option:selected').text();
    var sale_comments = $('#ctl00_ContentPlaceHolder1_TextBox1').val();
    var sale_cusid = getUrlVars()["cid"];
    var insUser = '2712BC15-BABE-4A23-9E3C-A8119695BD38'; //should add cookie value later
    var updUser = '2712BC15-BABE-4A23-9E3C-A8119695BD38'; //should add cookie value later
    if (sale_billNo == "") {
        $('#valBillNo').text("Enter the bill number");
    }
    else if (sale_billDt == "") {
        $('#valBillNo').text("");
        $('#valBillDate').text("Enter the billing date");
    }
    else {
        var params = { 'sGid': sale_guid,
            'sID': sale_ID,
            'sBNo': sale_billNo,
            'sBDt': sale_billDt,
            'sDlr': sale_dealer,
            'sBrnd': '',
            'sProd': sale_product,
            'sProdTyp': sale_productType,
            'sComnts': sale_comments,
            'sCusID': sale_cusid,
            'sInUsr': insUser,
            'sUpUsr': updUser,
            'sMdlNo': sale_modalno,
            'sCompNo': sale_compressorno,
            'sIndrNo':sale_indoorno,
            'sOutdrNo': sale_outdoorno,
            'sMdlNme': sale_modalName
        }
        var containerDIV = $('#divAddNewProductWizzard');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../_services/insertSale.ashx", params, 'html', fnSaleAddSuccessWizard, '', '', opts);
    }
    //    var parentDiv = $('#addCustomerWizzard');
    //    var moduleName = "../../_modules/ucAmcDetails.ascx";
    //    var _params = '&cid=' + cGCusID + '&sid=' + cGSaleID;
    //    var opts = { 'onsucess': onUCSalesLoaded, 'params': _params };
    //    loadModuler(moduleName, parentDiv, opts);
}
function fnSaleAddSuccessWizard(response) {
    if (response == "y") {
        $('#ui-dialog-title-divAddNewProductWizzard').text("AMC Details");
        $("#SalesAddForm").hide();
        $("#AmcAddForm").show();
        $('#txtAmcInvoiceNo').val(genDynmID());
        $('#txtInvoiceDt').datepicker({ dateFormat: "mm/dd/yy" }).datepicker("setDate", new Date());
        $('#txtWarrantyFrom').datepicker({ changeYear: true, changeMonth: true, dateFormat: "mm/dd/yy" }).datepicker("setDate", new Date());
        $('#expiryDate').datepicker({ changeYear: true, changeMonth: true, dateFormat: "mm/dd/yy", onSelect: function () {
            var d1 = new Date($('#txtWarrantyFrom').val()); // January 1st 2010
            var d2 = new Date($('#expiryDate').val()); // February 1st 2010
            var diff = d2 - d1;
            var daysRemaining = diff / 1000 / 60 / 60 / 24;
            $('#validityPeriod').text("Valid for " + daysRemaining + " days");
        }
        });
        disableTextbox();
    }
    else {

    }
}
function saveAMCDetailsWizzard() {
    //alert("save amc");
    var amc_guid = cGAmcID;
    var amc_id = $('#txtAmcInvoiceNo').val();
    var amc_invoice = $('#txtAmcInvoiceNo').val();
    var amc_invoiceDt = $('#txtInvoiceDt').val();
    var amc_warranty = $('#txtWarrantyFrom').val();
    var amc_expiry = $('#expiryDate').val();
    var amc_pmode = $('input:radio[name=paymode]:checked').val();
    var d1 = new Date($('#txtWarrantyFrom').val()); // January 1st 2010
    var d2 = new Date($('#expiryDate').val()); // February 1st 2010
    var diff = d2 - d1;
    var daysRemaining = diff / 1000 / 60 / 60 / 24;
    var amc_rate = $('#txtAMCRate').val();
    var num = (amc_rate / 365) * daysRemaining;

    var amc_amount = Math.ceil(num * 10) / 10;
    var amc_saleID = cGSaleID;
    var amc_cusID = getUrlVars()["cid"];
    var insUser = '2712BC15-BABE-4A23-9E3C-A8119695BD38'; //should add cookie value later
    var updUser = '2712BC15-BABE-4A23-9E3C-A8119695BD38'; //should add cookie value later

    var params = {
        'agid': amc_guid,
        'aid': amc_id,
        'ainvid': amc_invoice,
        'ainvDt': amc_invoiceDt,
        'awrnty': amc_warranty,
        'aexpiry': amc_expiry,
        'apmode': amc_pmode,
        'arate': amc_rate,
        'aAmt': amc_amount,
        'aSid': amc_saleID,
        'aCid': amc_cusID,
        'inUsr': insUser,
        'upUsr': updUser
    }
    var containerDIV = $('#divAddNewProductWizzard');
    var opts = { 'isLoadingGif': true, 'container': containerDIV };
    MakeJAjaxCall("../_services/insertAmc.ashx", params, 'html', fnAMCAddSuccessWizzard, '', '', opts);
}
function fnAMCAddSuccessWizzard(response) {
    if (response == "y") {
//        var parentDiv = $('#divAddNewProductWizzard');
//        var moduleName = "../../pages/amcInvoice.aspx";
//        var _params = '&cid=D8387409-8D81-43E7-8C8E-3FB97525E1C7' + '&sid=' + cGSaleID;
//        var opts = { 'onsucess': onUCPreviewLoaded, 'params': _params };
//        loadModuler(moduleName, parentDiv, opts);
        window.location.href = "../../pages/amcInvoice.aspx?cid=" + getUrlVars()["cid"] + "&sid=" + cGSaleID;
    }
    else {

    }
}

function suspendCustomer() {
    if ($("input:radio[name=gridSel]").is(':checked')) {
        var msg = 'Are you sure you want to suspend this customer account?';
        var opts = { 'AlertText': msg, 'OKTxt': 'Yes', 'CancelTxt': 'No', 'okClick': fnSuspendYes, 'cancelClick': fnSuspendNo, 'reqCancel': true };
        genAlertPOPUP(opts);
    }
    else {
        var msg = 'Please select the product to be suspended.';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': loadDummy, 'cancelClick': fnSuspendNo, 'reqCancel': false };
        genAlertPOPUP(opts);
    }
    
}
function fnSuspendYes() {
    if ($("input:radio[name=gridSel]:checked")) {
        //alert($("input:radio[name=gridSel]:checked").attr('saleid'));
        var selSaleGuid = $("input:radio[name=gridSel]:checked").attr('saleid');
        //suspendamc
        var params = { "sid": selSaleGuid }
        var containerDIV = $('#ulPaging');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../_services/suspendamc.ashx", params, 'html', fnSuspendResponse, '', '', opts);

    }
}
function fnSuspendResponse(response) {
    if (response == "y") {
        var msg = "Customer's AMC account suspended!";
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': fnSuspendDummy, 'cancelClick': fnSuspendDummy, 'reqCancel': false };
        genAlertPOPUP(opts);
    }
    else {
        var msg = 'Failed to suspend account. Please try again later.';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': fnSuspendDummy, 'cancelClick': fnSuspendDummy, 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}
function fnSuspendNo() {
}
function fnSuspendDummy() {
    loaducSales();
}
function loaducSales() {
    var parentDiv = $('#allCustomerGrid');
    var moduleName = "../../_modules/ucSalesLoad.ascx";
    var _params = '&cid=' + getUrlVars()["cid"];
    var opts = { 'onsucess': loadDummy, 'params': _params };
    loadModuler(moduleName, parentDiv, opts);
}
function loadDummy() {

}
function deleteCustomer() {
    var params = { "cid": getUrlVars()["cid"] }
    var containerDIV = $('#ulPaging');
    var opts = { 'isLoadingGif': true, 'container': containerDIV };
    MakeJAjaxCall("../_services/deleteCustomer.ashx", params, 'html', fnCustomerDeleteSuccess, '', '', opts);
}
function fnCustomerDeleteSuccess(response) {
    if (response == "y") {
        window.location.href = "../../pages/customer.aspx";
    }
    else {

    }
}
function fnDeleteCustomer() {
    var msg = "Are you sure you want to delete the customer?";
    var opts = { 'AlertText': msg, 'OKTxt': 'Yes', 'CancelTxt': 'No', 'okClick': deleteCustomer, 'cancelClick': loadDummy, 'reqCancel': true };
    genAlertPOPUP(opts);
}

function fnLogComplaint() {
    if ($("input:radio[name=gridSel]").is(':checked')) {
        //alert($("input:radio[name=gridSel]:checked").attr('saleid'));
        var cusid = getUrlVars()["cid"];
        var selSaleGuid = $("input:radio[name=gridSel]:checked").attr('saleid');
        $('#popComplaintWizzard').dialog({
            modal: true,
            height: 650,
            width: 750,
            open: function () {
                $('#formCustomerInfo').css('width', '715px');
                $('#ui-dialog-title-popComplaintWizzard').text("Log Complaint");
                //$('#txtAreaComments').empty();
                var parentDiv = $('#popComplaintWizzard');
                var moduleName = "../../_modules/ucComplaint.ascx";
                var _params = '&cid=' + cusid + '&sid=' + selSaleGuid;
                var opts = { 'onsucess': loadDummy, 'params': _params };
                loadModuler(moduleName, parentDiv, opts);
            }
        });
    }
    else {
        var msg = "Please select the product to log complaint?";
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': loadDummy, 'cancelClick': loadDummy, 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}

function fnSaveComplaint() {
    //alert("saveComplaint");
    var cid = getUrlVars()["cid"];
    var saleid = $("input:radio[name=gridSel]:checked").attr('saleid');
    var desc = $('#txtAreaComplaintDesc').val();
    var assignee = $('#ddlAssignee option:selected').text();
    var status = $('#ddlStatus option:selected').text();
    var issueid = genDynmID();
    var isSpare = $('input[type=radio][name=spare]:checked').val();
    var spareDetails = $('#spareDetails').val();
    var params = { "cid": cid, "sid": saleid, "desc": desc, "agnt": assignee, "stus": status, "issid": issueid,"isSpr":isSpare,"sprDet":spareDetails }
    var containerDIV = $('#popComplaintWizzard');
    var opts = { 'isLoadingGif': true, 'container': containerDIV };
    MakeJAjaxCall("../_services/logcomplaint.ashx", params, 'html', fnComplaintSaveSuccess, '', '', opts);
}
function fnStatusChange() {
    if ($('#ddlStatus :selected').val() == 3) {
        $('#isSpareDiv').show();
    }
    else {
        $('#isSpareDiv').hide();
    }
}
function fnClickYes() {
    $('#spareDetails').show();
}
function fnClickNo() {
    $('#spareDetails').hide();
}
function fnComplaintSaveSuccess(response) {
    if (response == "y") {
        $('#popComplaintWizzard').dialog('close');
        var msg = "Customer complaint has been registered successfully";
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': loadComplaints, 'cancelClick': loadDummy, 'reqCancel': false };
        genAlertPOPUP(opts);
    }
    else {
        var msg = "ERROR: Unable to save complaint. Please try again later.";
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': loadDummy, 'cancelClick': loadDummy, 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}

function loadComplaints() {
    var parentDiv = $('#gridCustomerComplaints');
    var moduleName = "../../_modules/ucComplaints.ascx";
    var _params = '&cid=' + getUrlVars()["cid"];
    var opts = { 'onsucess': loadDummy, 'params': _params };
    loadModuler(moduleName, parentDiv, opts);
}

function fnUpdateComplaint(obj) {
    //alert("update complaint");
    var selIssueID = $(obj).attr('compgid');
    var selCusID = $(obj).attr('cusid');
    var selSaleID = $(obj).attr('saleid');
    var issueID = $(obj).attr('issueid');
    //alert(complaintid);
    $('#popComplaintContainer').dialog({
        modal: true,
        height: 500,
        width: 750,
        open: function () {
            $('#ui-dialog-title-popComplaintContainer').text("Update Complaint");
            //$('#txtAreaComments').empty();
            var parentDiv = $('#popComplaintContainer');
            var moduleName = "../../_modules/ucUpdateComplaint.ascx";
            var _params = '&cmpid=' + issueID;
            var opts = { 'onsucess': function () {
                $('input[type=hidden][id*=issueCusID]').val(selCusID);
                $('input[type=hidden][id*=issueSaleID]').val(selSaleID);
                $('input[type=hidden][id*=issueID]').val(issueID);
                $('input[type=hidden][id*=issueGuid]').val(selIssueID);
            }, 'params': _params };
            loadModuler(moduleName, parentDiv, opts);
        }
    });
}
function fnUpdateComplaintWizzard() {
    //alert("saveComplaint");
    var desc = $('#txtUpdateAreaComplaintDesc').val();
    var assignee = $('#ddlUpdateAssignee option:selected').text();
    var status = $('#ddlUpdateStatus option:selected').text();
    if (status == "Closed" && $('input[type=hidden][id*=isSpare]').val() == "1") {
        var msg = "Customer is supplied with a spare device, please remember to collect it back";
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () {

            var params = { "cid": $('input[type=hidden][id*=issueCusID]').val(), "sid": $('input[type=hidden][id*=issueSaleID]').val(), "desc": desc, "agnt": assignee, "stus": status, "issid": $('input[type=hidden][id*=issueID]').val(), "isSpr": "No", "sprDet": "Issue Closed" }
            var containerDIV = $('#popComplaintWizzard');
            var opts = { 'isLoadingGif': true, 'container': containerDIV };
            MakeJAjaxCall("../_services/logcomplaint.ashx", params, 'html', fnComplaintSaveSuccessWizzard, '', '', opts);
        
        }, 'cancelClick': loadDummy, 'reqCancel': false };
        genAlertPOPUP(opts);
    }
    else {
        var params = { "cid": $('input[type=hidden][id*=issueCusID]').val(), "sid": $('input[type=hidden][id*=issueSaleID]').val(), "desc": desc, "agnt": assignee, "stus": status, "issid": $('input[type=hidden][id*=issueID]').val(), "isSpr": "No", "sprDet": "Issue Closed" }
        var containerDIV = $('#popComplaintWizzard');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../_services/logcomplaint.ashx", params, 'html', fnComplaintSaveSuccessWizzard, '', '', opts);
    }
}
function fnComplaintSaveSuccessWizzard(response) {
    if (response == "y") {
        $('#popComplaintContainer').dialog('close');
        var msg = "Customer complaint has been registered successfully";
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': loadComplaints, 'cancelClick': loadDummy, 'reqCancel': false };
        genAlertPOPUP(opts);
    }
    else {
        var msg = "ERROR: Unable to save complaint. Please try again later.";
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': loadDummy, 'cancelClick': loadDummy, 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}

function fnViewComplaintDetails(obj) {
    var cusid = $(obj).attr('cusid');
    var selid = $(obj).attr('saleid');
    var issueid = $(obj).attr('issueid');
    $('#popComplaintContainer').dialog({
        modal: true,
        height: 580,
        width: 750,
        open: function () {
            $('#ui-dialog-title-popComplaintContainer').text("Complaint Log");
            //$('#txtAreaComments').empty();
            var parentDiv = $('#popComplaintContainer');
            var moduleName = "../../_modules/ucComplaintDetails.ascx";
            var _params = '&cid=' + cusid + '&sid=' + selid + '&comid=' + issueid;
            var opts = { 'onsucess': '', 'params': _params };
            loadModuler(moduleName, parentDiv, opts);
        }
    });
}

function fnRenewAMC() {
    //alert("Renew AMC");
    if ($("input:radio[name=gridSel]").is(':checked')) {
        //alert("Renew AMC");
        var expiry = $("input:radio[name=gridSel]:checked").attr('expiry');
        var expdt = expiry.split('/');
        var d = new Date();
        var dat = d.getDate();
        var mon = d.getMonth()+1;
        var year = d.getFullYear();
        var todayDate = mon + "/" + dat + "/" + year;

        var d1 = new Date(year, mon, dat); // January 1st 2010
        var d2 = new Date(expdt[2], expdt[0], expdt[1]); // February 1st 2010
        var diff = d2 - d1;
        var daysRemaining = diff / 1000 / 60 / 60 / 24;
        if (daysRemaining <= 30) {

        }
        else {
            var msg = "AMC can be renewed at less than 30 days for expiry. Contract has " + daysRemaining + " days to expire";
            var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': loadDummy, 'cancelClick': loadDummy, 'reqCancel': false };
            genAlertPOPUP(opts);
        }



        //alert(todayDate);
    }
    else {
        var msg = "Please select the product to renew AMC.";
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': loadDummy, 'cancelClick': loadDummy, 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}